package com.pdg.dungeondelver.configuration;

import java.io.InputStream;

import org.newdawn.slick.SlickException;
import org.newdawn.slick.util.ResourceLoader;
import org.newdawn.slick.util.xml.XMLElement;
import org.newdawn.slick.util.xml.XMLElementList;
import org.newdawn.slick.util.xml.XMLParser;


public class Theme {

	private static final String TAG_NAME = "name";
	private static final String TAG_DESCRIPTION = "description";
	private static final String TAG_THUMBNAIL = "thumbnail";
	private static final String TAG_IMAGES_CONFIGURATION = "images-configuration";
	private static final String DEFAULT_IMAGES_CONFIGURATION = "";
	private static final String DEFAULT_THUMBNAIL = "";
	private static final String DEFAULT_DESCRIPTION = "";
	private static final String DEFAULT_NAME = "";
	private static final String TAG_PROPERTY_SERIALIZERS_CONFIGURATION = "property-serializers-configuration";
	private static final String TAG_PROPERTY_GROUPS_CONFIGURATION = "property-groups-configuration";
	private static final String TAG_ITEMS_CONFIGURATION = "items-configuration";
	private static final String TAG_CREATURES_CONFIGURATION = "creatures-configuration";
	private static final String TAG_TERRAINS_CONFIGURATION = "terrains-configuration";
	private static final String TAG_MESSAGES_CONFIGURATION = "messages-configuration";
	private static final String TAG_MAPS_CONFIGURATION = "maps-configuration";
	private Themes themes;
	private String propertySerializersConfiguration;
	private String propertyGroupsConfiguration;
	private String terrainsConfiguration;
	private String creaturesConfiguration;
	private String itemsConfiguration;
	private String messagesConfiguration;
	private DescriptorTable propertyGroups=null;
	private DescriptorTable items=null;
	private DescriptorTable creatures=null;
	private DescriptorTable terrains=null;
	private DescriptorTable messages = null;
	private String mapsConfiguration=null;
	private MapManager mapManager = null;
	
	public String getMapsConfiguration(){
		return mapsConfiguration;
	}
	
	public MapManager getMaps(){
		if(mapManager==null){
			try {
				mapManager  = new MapManager(this,getMapsConfiguration());
			} catch (MapManagerLoadException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				mapManager=null;
			}
		}
		return mapManager;
	}
	
	public String getPropertyGroupsConfiguration(){
		return propertyGroupsConfiguration;
	}
	
	public String getTerrainsConfiguration(){
		return terrainsConfiguration;
	}
	
	public String getMessagesConfiguration(){
		return messagesConfiguration;
	}
	
	public String getItemsConfiguration(){
		return itemsConfiguration;
	}
	
	public String getCreaturesConfiguration(){
		return creaturesConfiguration;
	}
	
	public DescriptorTable getPropertyGroups(){
		if(propertyGroups==null){
			try {
				propertyGroups = new DescriptorTable(this, getPropertyGroupsConfiguration());
			} catch (DescriptorTableLoadException e) {
				e.printStackTrace();
				propertyGroups=null;
			}
		}
		return propertyGroups;
	}
	
	public DescriptorTable getMessages(){
		if(messages==null){
			try {
				messages = new DescriptorTable(this, getMessagesConfiguration());
			} catch (DescriptorTableLoadException e) {
				e.printStackTrace();
				messages=null;
			}
		}
		return messages;
	}
	
	public DescriptorTable getItems(){
		if(items==null){
			try {
				items = new DescriptorTable(this, getItemsConfiguration());
			} catch (DescriptorTableLoadException e) {
				e.printStackTrace();
				items=null;
			}
		}
		return items;
	}
	
	public DescriptorTable getCreatures(){
		if(creatures==null){
			try {
				creatures = new DescriptorTable(this, getCreaturesConfiguration());
			} catch (DescriptorTableLoadException e) {
				e.printStackTrace();
				creatures=null;
			}
		}
		return creatures;
	}
	
	public DescriptorTable getTerrains(){
		if(terrains==null){
			try {
				terrains = new DescriptorTable(this, getTerrainsConfiguration());
			} catch (DescriptorTableLoadException e) {
				e.printStackTrace();
				terrains=null;
			}
		}
		return terrains;
	}
	
	public Themes getThemes(){
		return themes;
	}

	public Theme(Themes theThemes,String ref) throws ThemeLoadException {
		setThemes(theThemes);
		if(!loadFromXml(ref)){
			throw new ThemeLoadException();
		}
	}

	private void setThemes(Themes theThemes) {
		themes = theThemes;
	}

	private boolean loadFromXml(String ref) {
		XMLParser parser = new XMLParser();
		try {
			InputStream stream = ResourceLoader.getResourceAsStream(ref);
			XMLElement root = parser.parse(ref,stream);
			XMLElementList children = root.getChildren();
			for(Integer index=0;index<children.size();++index){
				XMLElement child = children.get(index);
				if(child.getName().compareTo(TAG_NAME)==0){
					setName(child.getContent());
				}else if(child.getName().compareTo(TAG_DESCRIPTION)==0){
					setDescription(child.getContent());
				}else if(child.getName().compareTo(TAG_THUMBNAIL)==0){
					setThumnail(child.getContent());
				}else if(child.getName().compareTo(TAG_IMAGES_CONFIGURATION)==0){
					setImagesConfiguration(child.getContent());
				}else if(child.getName().compareTo(TAG_PROPERTY_SERIALIZERS_CONFIGURATION)==0){
					setPropertySerializersConfiguration(child.getContent());
				}else if(child.getName().compareTo(TAG_PROPERTY_GROUPS_CONFIGURATION)==0){
					setPropertyGroupsConfiguration(child.getContent());
				}else if(child.getName().compareTo(TAG_ITEMS_CONFIGURATION)==0){
					setItemsConfiguration(child.getContent());
				}else if(child.getName().compareTo(TAG_CREATURES_CONFIGURATION)==0){
					setCreaturesConfiguration(child.getContent());
				}else if(child.getName().compareTo(TAG_TERRAINS_CONFIGURATION)==0){
					setTerrainsConfiguration(child.getContent());
				}else if(child.getName().compareTo(TAG_MESSAGES_CONFIGURATION)==0){
					setMessagesConfiguration(child.getContent());
				}else if(child.getName().compareTo(TAG_MAPS_CONFIGURATION)==0){
					setMapsConfiguration(child.getContent());
				}
			}
			return true;
		} catch (SlickException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	private void setMapsConfiguration(String content) {
		mapsConfiguration = content;
		
	}

	private void setMessagesConfiguration(String content) {
		messagesConfiguration = content;
		
	}

	private void setTerrainsConfiguration(String content) {
		terrainsConfiguration = content;
		
	}

	private void setCreaturesConfiguration(String content) {
		creaturesConfiguration = content;
		
	}

	private void setItemsConfiguration(String content) {
		itemsConfiguration = content;
		
	}

	private void setPropertyGroupsConfiguration(String content) {
		propertyGroupsConfiguration = content;
		
	}

	private PropertySerializers generators = null;
	public PropertySerializers getPropertySerializers(){
		if(generators==null){
			try {
				generators = new PropertySerializers(this,getPropertySerializersConfiguration());
			} catch (PropertySerializersLoadException e) {
				e.printStackTrace();
				generators = null;
			}
		}
		return generators;
	}
	
	public String getPropertySerializersConfiguration(){
		return propertySerializersConfiguration;
	}
	
	private void setPropertySerializersConfiguration(String content) {
		propertySerializersConfiguration = content;
		
	}

	private ImageManager imageManager = null;
	public ImageManager getImageManager(){
		if(imageManager==null){
			try {
				imageManager = new ImageManager(this,getImagesConfiguration());
			} catch (ImageManagerLoadException e) {
				e.printStackTrace();
				imageManager = null;
			}
		}
		return imageManager;
	}

	private String imagesConfiguration = DEFAULT_IMAGES_CONFIGURATION;
	private void setImagesConfiguration(String content) {
		imagesConfiguration = content;
	}
	public String getImagesConfiguration(){
		return imagesConfiguration;
	}

	private String thumbnail = DEFAULT_THUMBNAIL;
	private void setThumnail(String content) {
		thumbnail = content;
	}
	public String getThumbnail(){
		return thumbnail;
	}

	
	private String description = DEFAULT_DESCRIPTION;
	private void setDescription(String content) {
		description = content;
	}
	public String getDescription(){
		return description;
	}

	private String name = DEFAULT_NAME;
	private void setName(String content) {
		name  = content;
	}
	public String getName(){
		return name;
	}
	

}
